package com.demo.oa.biz.impl;

import com.demo.oa.biz.ClaimVoucherBiz;
import com.demo.oa.dao.ClaimVoucherDao;
import com.demo.oa.dao.ClaimVoucherItemDao;
import com.demo.oa.dao.DealRecordDao;
import com.demo.oa.dao.EmployeeDao;
import com.demo.oa.entity.ClaimVoucher;
import com.demo.oa.entity.ClaimVoucherItem;
import com.demo.oa.entity.DealRecord;
import com.demo.oa.entity.Employee;
import com.demo.oa.global.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("cliamVoucherBiz")
public class ClaimVoucherBizImpl implements ClaimVoucherBiz {

    @Autowired
    @Qualifier("claimVoucherDao")
    private ClaimVoucherDao claimVoucherDao;

    @Autowired
    @Qualifier("claimVoucherItemDao")
    private ClaimVoucherItemDao claimVoucherItemDao;

    @Autowired
    @Qualifier("dealRecordDao")
    private DealRecordDao dealRecordDao;

    @Autowired
    @Qualifier("employeeDao")
    private EmployeeDao employeeDao;

    @Override
    public void save(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        // 设置创建时间
        claimVoucher.setCreateTime(new Date());
        // 设置待处理人为自己
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        // 设置报销单状态
        claimVoucher.setStatus(Constant.CLAIMVOUCHER_CREATED);
        // mapper.xml 里写了自动注入主键，insert 执行的时候 mybatis 会自动往实体类里注入 id
        claimVoucherDao.insert(claimVoucher);

        // 保存报销单条目
        for(ClaimVoucherItem item:items) {
            item.setClaimVoucherId(claimVoucher.getId());
            claimVoucherItemDao.insert(item);
        }
    }

    @Override
    public ClaimVoucher get(int id) {
        return claimVoucherDao.select(id);
    }

    @Override
    public List<ClaimVoucherItem> getItems(int cvid) {
        return claimVoucherItemDao.selectByClaimVoucher(cvid);
    }

    @Override
    public List<DealRecord> getRecords(int cvid) {
        return dealRecordDao.selectByClaimVoucher(cvid);
    }

    @Override
    public List<ClaimVoucher> getForSelf(String sn) {
        return claimVoucherDao.selectByCreateSn(sn);
    }

    @Override
    public List<ClaimVoucher> getForDeal(String sn) {
        return claimVoucherDao.selectByNextDealSn(sn);
    }

    @Override
    public void update(ClaimVoucher claimVoucher, List<ClaimVoucherItem> items) {
        // 更新报销单的基本信息
        claimVoucher.setNextDealSn(claimVoucher.getCreateSn());
        claimVoucher.setStatus(Constant.CLAIMVOUCHER_CREATED);
        claimVoucherDao.update(claimVoucher);

        // 更新条目集合
        // 获取数据库里已有的集合
        List<ClaimVoucherItem> olds = claimVoucherItemDao.selectByClaimVoucher(claimVoucher.getId());
        for (ClaimVoucherItem old:olds) {
            Boolean isHave = false;
            for (ClaimVoucherItem item:items) {
                if(item.getId()==old.getId()){
                    // 存在此条报销单的情况
                    isHave=true;
                    break;
                }
            }
            // 如果老的报销单列表中存在，但是新的报销单列表中不存在，说明是删除操作
            if(!isHave){
                claimVoucherItemDao.delete(old.getId());
            }
        }

        for (ClaimVoucherItem item:items) {
            item.setClaimVoucherId(claimVoucher.getId());
            if (item.getId() != null && item.getId() > 0) {
                // 如果 id 是大于零的，说明是更新操作
                claimVoucherItemDao.update(item);
            } else {
                // 否则是更新操作
                claimVoucherItemDao.insert(item);
            }
        }
    }

    @Override
    public void submit(int id) {
        ClaimVoucher claimVoucher = claimVoucherDao.select(id);
        Employee employee = employeeDao.select(claimVoucher.getCreateSn());

        // 更改状态为：待审核
        claimVoucher.setStatus(Constant.CLAIMVOUCHER_SUBMIT);
        // 得到与当前员工在统一部门的、属于部门经理的人人员编号
        claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(employee.getDepartmentSn(),Constant.POST_FM).get(0).getSn());
        // 更新报销单数据
        claimVoucherDao.update(claimVoucher);

        // 进行记录的保存
        DealRecord dealRecord = new DealRecord();
        // 设置处理方式为：提交
        dealRecord.setDealWay(Constant.DEAL_SUBMIT);
        // 设置当前处理人
        dealRecord.setDealSn(employee.getSn());
        // 报销单 ID
        dealRecord.setClaimVoucherId(id);
        // 提交状态：已提交
        dealRecord.setDealResult(Constant.CLAIMVOUCHER_SUBMIT);
        // 处理时间
        dealRecord.setDealTime(new Date());
        // 备注
        dealRecord.setComment("无");
        // 保存
        dealRecordDao.insert(dealRecord);
    }

    @Override
    public void deal(DealRecord dealRecord) {
        ClaimVoucher claimVoucher = claimVoucherDao.select(dealRecord.getClaimVoucherId());
        Employee employee = employeeDao.select(dealRecord.getDealSn());
        dealRecord.setDealTime(new Date());

        if(dealRecord.getDealWay().equals(Constant.DEAL_PASS)){ // 通过
            // 1. 报销单报销金额小于限制金额
            // 2. 报销单批准人为总经理
            if(claimVoucher.getTotalAmount()<=Constant.LIMIT_CHECK || employee.getPost().equals(Constant.POST_GM)){
                claimVoucher.setStatus(Constant.CLAIMVOUCHER_APPROVED); // 状态改变为已审核
                claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null,Constant.POST_CASHIER).get(0).getSn()); // 审核人转交给财务

                dealRecord.setDealResult(Constant.CLAIMVOUCHER_APPROVED);
            }else{ // 需要复审的
                claimVoucher.setStatus(Constant.CLAIMVOUCHER_RECHECK);
                claimVoucher.setNextDealSn(employeeDao.selectByDepartmentAndPost(null,Constant.POST_GM).get(0).getSn()); // 待处理人为总经理

                dealRecord.setDealResult(Constant.CLAIMVOUCHER_RECHECK);
            }
        }else if(dealRecord.getDealWay().equals(Constant.DEAL_BACK)){ // 打回
            claimVoucher.setStatus(Constant.CLAIMVOUCHER_BACK);
            claimVoucher.setNextDealSn(claimVoucher.getCreateSn()); // 处理人为报销单的创建者

            dealRecord.setDealResult(Constant.CLAIMVOUCHER_BACK);
        }else if(dealRecord.getDealWay().equals(Constant.DEAL_REJECT)){ // 拒绝
            claimVoucher.setStatus(Constant.CLAIMVOUCHER_TERMINATED);
            claimVoucher.setNextDealSn(null);

            dealRecord.setDealResult(Constant.CLAIMVOUCHER_TERMINATED);
        }else if(dealRecord.getDealWay().equals(Constant.DEAL_PAID)){ // 打款
            claimVoucher.setStatus(Constant.CLAIMVOUCHER_PAID);
            claimVoucher.setNextDealSn(null);

            dealRecord.setDealResult(Constant.CLAIMVOUCHER_PAID);
        }

        claimVoucherDao.update(claimVoucher);
        dealRecordDao.insert(dealRecord);

    }
}
